package com.crazy.sso.website.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.core.Constants;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by creazier.huang on 16/4/17.
 */
public class SpringMVCInterceptor extends HandlerInterceptorAdapter implements InitializingBean,Constants{

    private final Logger logger = LoggerFactory.getLogger(SpringMVCInterceptor.class);

    @Value("${web.staticPath}")
    private String staticPath;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("start request >> " + request.getRequestURI() + " by " + request.getHeader("user-agent"));
        request.setAttribute("uri", request.getRequestURI());
        request.setAttribute("staticPath",staticPath);
        request.setAttribute("version","1.0.0");

        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }


    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
